package com.leetcode.Offer;

public class JZ26_2 {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        boolean flag = false;
        if (A != null && B != null) {
            if (A.val == B.val) {
                // check tree
                flag = checkTree(A, B);
            }
            if (!flag) {
                flag = isSubStructure(A.left, B);
            }
            if (!flag) {
                flag = isSubStructure(A.right, B);
            }
        }
        return flag;
    }

    public boolean checkTree(TreeNode A, TreeNode B) {
        if (B == null) {
            return true;
        }
        if (A == null) {
            return false;
        }
        if (A.val == B.val) {
            return checkTree(A.left, B.left) && checkTree(A.right, B.right);
        }
        return false;
    }
}
